/*
 * KnowledgeBasesDao.java
 *
 * Created on November 9, 2008, 7:38 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jollylengkono.skripsi.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jollylengkono.skripsi.iface.Crud;
import jollylengkono.skripsi.model.KnowledgeBases;

/**
 *
 * @author JollkY
 */
public class KnowledgeBasesDao implements Crud {
    
    private int state;
    private KnowledgeBases knowledgeBase;
    private List knowledgeBases;
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("kmPU");
    private EntityManager em = emf.createEntityManager();
    
    /** Creates a new instance of KnowledgeBasesDao */
    public KnowledgeBasesDao() {
    }

    
    public int create(Object obj) {
        try {
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    
    public List<Object> getList() {
        knowledgeBases = new ArrayList();
        try {
            em.getTransaction().begin();
            knowledgeBases = em.createQuery("SELECT kb FROM KnowledgeBases kb").getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return knowledgeBases;
    }
    
    public List getListByOwner(long idOwner) {
        knowledgeBases = new ArrayList();
        try {
            em.getTransaction().begin();
            knowledgeBases = em.createQuery("SELECT kb FROM KnowledgeBases kb " +
                    "WHERE kb.owner.id="+idOwner).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return knowledgeBases;
    }
    
    public List getListBySubject(long idSubject) {
        knowledgeBases = new ArrayList();
        try {
            em.getTransaction().begin();
            knowledgeBases = em.createQuery("SELECT kb FROM KnowledgeBases kb " +
                    "WHERE kb.subject.id="+idSubject).getResultList();
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return knowledgeBases;
    }

    
    public Object read(long id) {
        knowledgeBase = new KnowledgeBases();
        try {
            em.getTransaction().begin();
            knowledgeBase = em.find(KnowledgeBases.class, id);
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
        return knowledgeBase;
    }

    public int update(Object obj) {
        knowledgeBase = new KnowledgeBases();
        try {
            KnowledgeBases objKb = (KnowledgeBases) obj;
            em.getTransaction().begin();
            knowledgeBase = em.find(KnowledgeBases.class, objKb.getId());
            knowledgeBase.setDownloaded(objKb.getDownloaded());
            knowledgeBase.setFullContent(objKb.getFullContent());
            knowledgeBase.setHasDocument(objKb.getHasDocument());
            knowledgeBase.setOwner(objKb.getOwner());
            knowledgeBase.setPublishes(objKb.getPublishes());
            knowledgeBase.setShortDescription(objKb.getShortDescription());
            knowledgeBase.setStatus(objKb.getStatus());
            knowledgeBase.setSubject(objKb.getSubject());
            knowledgeBase.setTitle(objKb.getTitle());
            knowledgeBase.setUpdates(objKb.getUpdates());
            knowledgeBase.setWrites(objKb.getWrites());
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }

    public int delete(long id) {
        knowledgeBase = new KnowledgeBases();
        try {
            em.getTransaction().begin();
            knowledgeBase = em.find(KnowledgeBases.class, id);
            em.remove(knowledgeBase);
            em.getTransaction().commit();

            this.state = 1;
        } catch (Exception ex) {
            ex.printStackTrace();
            em.getTransaction().rollback();

            this.state = 0;
        } finally {
            em.close();
        }
        return state;
    }
    
}
